<template>
	<view class="content" v-if="pageShow">
		<view class="padding ffffff">
			<view class="flexleft">
				<view class="color_san text_x" style="margin-right: 80rpx;">收货人</view>
				<view class="inputBox">
					<input class="text_a" type="text" placeholder="请输入收货人姓名"
						placeholder-style="font-size: 30rpx;font-weight: 500;color: #999999;" v-model="name">
				</view>
			</view>
			<view class="flexleft">
				<view class="color_san text_x margin_right6">手机号码</view>
				<view class="inputBox">
					<input class="text_a" type="text" placeholder="请输入手机号码"
						placeholder-style="font-size: 30rpx;font-weight: 500;color: #999999;" v-model="mobile">
				</view>
			</view>
			<view class="flexleft">
				<view class="color_san text_x margin_right6">选择地址</view>
				<uni-data-picker @change="bindPickerChange" :localdata="items" placeholder="请选择住址" popup-title="请选择地区"
					v-model="addressinfo.county" class="inputBox">
					<text class="inputs1 text_g color_jiu" v-if="!area.length">选择省、市、县</text>
					<text class="inputs1" v-else>{{ area[0] }}/{{ area[1] }}/{{ area[2] }}</text>
				</uni-data-picker>
			</view>
			<view class="flexleft">
				<view class="color_san text_x margin_right6">详细地址</view>
				<view class="inputBox">
					<input class="text_a" type="text" placeholder="请输入详细地址"
						placeholder-style="font-size: 30rpx;font-weight: 500;color: #999999;" v-model="address">
				</view>
			</view>
		</view>
		<!-- 底部 -->
		<view class="footer ffffff padding">
			<view class="footerButton" @click="addaddress">保存</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	import citys from "@/static/city/city.js"
	export default {
		data() {
			return {
				is_default: false,
				area: "",
				addressinfo: {
					province: "", //省
					city: "", //市
					county: "", //区
				},
				items: citys.data,
				address: "",
				name: "",
				mobile: "",
				pageShow:false
			}
		},
		onLoad() {
			this.getDetail()
		},
		methods: {
			getDetail(){
				httpRequest.request('/api/shop.index/getform', 'GET', {}).then(res => {
					if(res.data.return_region){
						this.area  = res.data.return_region.split("/");
					}
					this.mobile = res.data.return_phone
					this.name = res.data.return_name
					this.address = res.data.return_addr
					this.pageShow=true
				})
			},
			isMobile(value) { //手机号校验
				let a = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value);
				return a;
			},
			addaddress() { //添加地址
				uni.showLoading({
					title: '请稍候...',
					mask: true
				})
				let _this = this;
				let mobile = _this.mobile;
				if (_this.name == '') {
					uni.hideLoading()
					httpRequest.toast('请输入收货人名字');
					return false;
				}
				if (_this.mobile == "") {
					uni.hideLoading()
					httpRequest.toast('请输入收货人手机号');
					return false;
				}
				if (!_this.isMobile(_this.mobile)) {
					uni.hideLoading()
					httpRequest.toast("手机号输入有误")
					return false
				}
				if (!_this.area.length) {
					uni.hideLoading()
					httpRequest.toast('请选择地址');
					return false;
				}
				if (_this.address == false) {
					uni.hideLoading()
					httpRequest.toast('请输入详细地址');
					return false;
				}
				let data = {
					return_name: _this.name,
					return_phone: _this.mobile,
					return_addr: _this.address,
					return_region: this.area.join("/"),
				}
				httpRequest.request('/api/shop.index/save', 'POST', data, false, false, true).then(res => {
					uni.hideLoading()
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					}
				})
			},
			bindPickerChange(val) {
				let area = val.detail.value
				this.area = [area[0].text, area[1].text, area[2].text];
				this.addressinfo.province = area[0].text;
				this.addressinfo.city = area[1].text;
				this.addressinfo.county = area[2].text;
				// this.addressinfo.province = area[0].value;
				// this.addressinfo.city = area[1].value;
				// this.addressinfo.county = area[2].value;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.inputBox {
		padding: 20rpx 0;
		border-bottom: 2rpx solid #F1F1F1;
		flex: 1;
		// margin-left: 60rpx;
	}
</style>